package io.modifier.basic.compose.paging

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import io.modifier.basic.compose.animation.LoadingSpinner
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.widget.comm.AppBtn
import io.modifier.basic.compose.widget.comm.EmptyPage
import io.modifier.basic.compose.widget.comm.LoadingPage
import zlc.season.yasha.compose.swiperefresh.SwipeRefresh
import zlc.season.yasha.compose.swiperefresh.SwipeRefreshIndicator
import zlc.season.yasha.compose.swiperefresh.rememberSwipeRefreshState

@Composable
fun Ref(onRef: () -> Unit, content: @Composable () -> Unit) {
    val refreshState = rememberSwipeRefreshState(isRefreshing = false)

    SwipeRefresh(
        state = refreshState,
        indicator = { s, trigger ->
            SwipeRefreshIndicator(s, trigger, contentColor = AppThemeHolder.colors.appColor)
        },
        onRefresh = { onRef.invoke() },
    ) {
        content()
    }
}


@Composable
fun PagingRefStatus(
    loadState: LoadState,
    itemCount: Int,
    retryText: String = "retry",
    onRetry: () -> Unit = {},
    content: @Composable () -> Unit
) {

    when (loadState) {
        is LoadState.Loading -> {
            LoadingPage()
        }

        is LoadState.Error -> {
            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                Box(modifier = Modifier.fillMaxWidth(.43f)) {
                    AppBtn(retryText) {
                        onRetry.invoke()
                    }
                }
            }
        }

        is LoadState.NotLoading -> {
            if (itemCount == 0)
                EmptyPage(onClick = onRetry)
            else
                Ref(onRef = onRetry, content = content)
        }
    }

}

@Composable
fun <T : Any> LazyPagingItems<T>.WithPagingRefStatus(content: @Composable () -> Unit) {
    PagingRefStatus(
        loadState = this.loadState.refresh,
        itemCount = this.itemCount,
        onRetry = {
            this.refresh()
        },
        content = content,
    )
}

@Composable
fun PagingMoreStatus(
    loadState: LoadState,
    failedDesText: String = "failed",
    noMoreDataDesText: String = "no more data",
    ts: TextStyle = AppThemeHolder.textStyle.title,
    onRetry: () -> Unit = {}
) {
    Box(
        Modifier
            .fillMaxWidth()
            .height(70.dp),
        contentAlignment = Alignment.Center,
    ) {
        when (loadState) {
            is LoadState.Loading -> {
                LoadingSpinner()
            }

            is LoadState.Error -> {
                AppBtn (failedDesText) {
                    onRetry.invoke()
                }
            }

            is LoadState.NotLoading -> {
                Text("-- $noMoreDataDesText --", style = ts)
            }
        }
    }
}

@Composable
fun <T : Any> LazyPagingItems<T>.WithPagingMoreStatus() {
    PagingMoreStatus(
        loadState = this.loadState.append,
        onRetry = {
            this.refresh()
        },
    )
}
